package dawnland03.model.entity.attribute;

import dawnland03.data.map.model.MatrixDirection;
import dawnland03.data.map.model.MatrixCell;
import dawnland03.service.GeneralFacade;

/**
 * This object contains informations related to the position of an entity.
 * <p/>
 * User: Petru Obreja (obrejap@yahoo.com)
 * Date: Feb 13, 2010
 * Time: 10:05:20 PM
 */
public class Position{
    private Integer x;
    private Integer y;
    private MatrixCell matrixCell;
    private MatrixDirection matrixDirection;

    public Position(Integer x, Integer y) {
        this(x, y, MatrixDirection.UP);
    }

    public Position(Integer x, Integer y, MatrixDirection matrixDirection) {
        setCoordinates(x, y);
        this.matrixDirection = matrixDirection;
    }

    public Integer getX() {
        return x;
    }

    public Integer getY() {
        return y;
    }

    public MatrixCell getMatrixCell() {
        return matrixCell;
    }

    public final void setCoordinates(Integer x, Integer y) {
        this.x = x;
        this.y = y;
        matrixCell = GeneralFacade.getMatrix().getMatrixCell(x, y);
    }

    public void setCoordinates(Integer matrixCellId) {
        matrixCell = GeneralFacade.getMatrix().getMatrixCell(matrixCellId);
        x = matrixCell.getX();
        y = matrixCell.getY();
    }

    public MatrixDirection getMatrixDirection() {
        return matrixDirection;
    }

    public void setMatrixDirection(MatrixDirection matrixDirection) {
        this.matrixDirection = matrixDirection;
    }

    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Position position = (Position) o;

        if (!x.equals(position.x)) return false;
        if (!y.equals(position.y)) return false;

        return true;
    }

    public int hashCode() {
        int result;
        result = x.hashCode();
        result = 31 * result + y.hashCode();
        return result;
    }

    public String toString() {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("P(").append(x).append("-").append(y).append(")");
        return stringBuilder.toString();
    }
}
